<?php
include 'D:/dingjiangbbs/common/home.php';
//屏蔽notice错误
error_reporting(E_ALL ^ E_NOTICE);

/******获取传递过来的值************************/
//var_dump($_POST);

//获取 post 和 cookie 的值
$title = '  ';
$reply = $_POST['reply'];

//id路径:公共页面-->个人主页-->回复栏-->回复的后台
$id    = $_POST['id'];
//$tid = $_COOKIE['id'];
$authorid = $_COOKIE['uid'];
//var_dump($id);

//获取发帖人ip
$ip = $_SERVER['REMOTE_ADDR'];
if ($ip = '::1') {

	$ip = '127.0.0.1';

}else{

	$ip = $_SERVER['REMOTE_ADDR'];
}
$ip    = ip2long($ip);

/*********数据库操作***********************/
$table = DB_TABLE_DETAIL;

//通过id查询classid
//$id = $_COOKIE['id'];
$fields = 'classid';
$where = "where id=$id";
$res_reply1 = select($link,$table,$fields,$where);
$classid = $res_reply1[0]['classid'];

//将回复内容插入数据库
$data  = [
	
	'first'    => 0,
	'title'    => "$title",
	'classid'  => $classid,
	'tid'	   => $id,
	'addip'    => $ip,
	'addtime'  => time(),
	'content'  => "$reply",
	'authorid' => $authorid
];
//var_dump($data);
$res_reply = insert($link,$table,$data);
//var_dump($res_reply);
	
/**********计算回复数**********************/
//查询数据库，回复数
$table  = DB_TABLE_DETAIL;
$fields = 'replycount';
$where  = "where id=$id"; 
$reply_count = select($link,$table,$fields,$where);
//var_dump($reply_count);
$reply_count = $reply_count[0]['replycount'];

//回复数加一，并插入数据库
$data = ['replycount' => $reply_count+1];
//var_dump($data);
$where = "id=$id";
$res_reply = update($link,$table,$data,$where);
//var_dump($res_reply);
/*******************************************/
/*******回复积分+2******************************/
//查询出用户原积分信息
$num    = 2;
$table  = DB_TABLE_USER;
$fields = 'grade';
$where  = "where uid=$authorid";
$res_grade1 = select($link,$table,$fields,$where);
$grade = $res_grade1[0]['grade'];
//var_dump($res_grade1);
//var_dump($grade);

//给用户加积分
$data   = ['grade' => $grade+$num];
$where  = "uid=$authorid";
$res_grade2 = update($link,$table,$data,$where);
//var_dump($res_grade2);
/************************************************/

//跳转页面
echo '回复成功,积分+'.$num;
header('refresh:2;url='.WEB_SITE.'personal.php?id='.$id);

//关闭资源
mysqli_close($link);